//
//  CollectionExtension.swift
//  ezyform
//
//  Created by MoennigQ on 24/5/18.
//  Copyright © 2018 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import WebKit
extension UIImage {
    //MARK: - 获取缩略图
    func generateThumbnailImage()->UIImage{
        var resultImage: UIImage = self
        if #available(iOS 15.0, *) {
            if let image = self.preparingThumbnail(of: APPConfig.thumbnailSize){
                resultImage = image
            }
        } else {
            let format = UIGraphicsImageRendererFormat()
            format.opaque = true
            format.scale = 1
            let renderer = UIGraphicsImageRenderer(size: APPConfig.thumbnailSize)
            resultImage = renderer.image{ context in
                self.draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: APPConfig.thumbnailSize))
            }
        }
        return resultImage
    }
}

extension UIColor {
    convenience init(rgb: UInt) {
        self.init(
            red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgb & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }
}
extension WKWebView {
    func exportHTMLToImage() ->UIImage {
        //
        let format = UIGraphicsImageRendererFormat()
        format.opaque = true
        format.scale = UIScreen.main.scale
        let renderer = UIGraphicsImageRenderer(size: self.scrollView.contentSize,format: format)
        let image = renderer.image{ context in
            self.scrollView.layer.render(in: context.cgContext)
        }
        
        
        return image
    }
    func snapshot(){
        let snapConfig = WKSnapshotConfiguration()
        snapConfig.rect = self.bounds
        //获取截图
        self.takeSnapshot(with: snapConfig) { (image, err) in
            if let _ = image {
                UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
            }
        }
    }
}
